package impl.repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import dao.AccessDatabase;
import domain.Address;
import domain.Billing;
import domain.CartItem;
import domain.Customer;
import domain.Order;
import domain.Shipping;
import exception.InvalidCepException;
import interfaces.repository.AddressRepository;

public class StubAddresRepositoryImpl implements AddressRepository {

	@Override
	public Address findById(int idAddress) {
		Address endereco = null;
		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM address WHERE idAddress='"
							+ idAddress + "'");

			endereco = new Address(rs.getInt("idAddress"), rs.getString("field1"), rs.getString("field2"), rs.getString("field3"), rs.getString("city"), rs.getString("state"), rs.getString("zip"), rs.getString("country"));
			
			con.close();

			return endereco;
		} catch (SQLException e) {
			return null;
		}

	}

	@Override
	public boolean add(Address address) {
		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("INSERT INTO `address` (`field1`,`field2`,`field3`,`city`,`state`,`zip`,`country`) VALUES ('"
					+ address.getField1()
					+ "','"
					+ address.getField2()
					+ "','"
					+ address.getField3()
					+ "','"
					+ address.getCity()
					+ "','"
					+ address.getState()
					+ "','"
					+ address.getZip()
					+ "','"
					+ address.getCountry()
					+ "');");
			
			con.close();
			return true;
		} catch (SQLException e) {
			System.out
					.println("nao foi possivel prosseguir com o registro" + e);
		}
		return false;
	}

	@Override
	public boolean edit(Address before, Address after) {
		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("UPDATE `address` SET `city`='" + after.getCity()+ "', `state`='" + after.getState() + "', `zip`='"+ after.getZip() + "', `field1`='" + after.getField1() + "', `field2`='" + after.getField2() + "', `field3`='" + after.getField3() + "', `country`='" + after.getCountry() + "' WHERE `idAddress`='"	+ before.getId() + "'");
			con.close();
			return true;
		} catch (Exception t) {
			JOptionPane.showMessageDialog(null, t.getMessage());
			return false;
		}

	}

	@Override
	public boolean delete(int address) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `address` WHERE `idAddress`='"
					+ address + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

}
